package com.example.aop.proxy;

import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        //使用CGLib完成代理
//        //目标对象
//        RealHouseSubject target = new RealHouseSubject();
//        //代理接口
//        HouseSubject houseSubject = (HouseSubject) Enhancer.create(target.getClass(), new CGLibMethodInterceptor(target));
//        houseSubject.rentHouse();
//        houseSubject.saleHouse();

        //代理类
        HouseSubject targer = new RealHouseSubject();
        RealHouseSubject houseSubject = (RealHouseSubject) Enhancer.create(targer.getClass(), new CGLibMethodInterceptor(targer));
        houseSubject.rentHouse();
        houseSubject.saleHouse();


    }

    public static void main1(String[] args) {
//        //静态代理
//        HouseProxy proxy = new HouseProxy(new RealHouseSubject());
//        proxy.rentHouse();

        //JDK动态代理接口
        RealHouseSubject targer = new RealHouseSubject();
        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(
                targer.getClass().getClassLoader(),
                new Class[]{HouseSubject.class},
                new JDKInvocationHandler(targer));
        proxy.rentHouse();
        proxy.saleHouse();
    }
}
















